package net.ddrjw.soft.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import net.ddrjw.soft.dao.UserDao;
import net.ddrjw.soft.db.DBCPConnection;
import net.ddrjw.soft.entity.AstroType;
import net.ddrjw.soft.entity.BloodType;
import net.ddrjw.soft.entity.User;
import net.ddrjw.soft.log.LogDog;

public class UserImpl implements UserDao {

	// 定义所需要的资源
	private Connection connection = null;
	private Statement statement = null;
	private PreparedStatement prStatement = null;
	private ResultSet reSet = null;

	// 构造方法实现数据库连接的初始化
	public UserImpl() {
		connection = DBCPConnection.getconnection();
		if (connection != null) {
			LogDog.logger.info("dbConnection success !");
		}
	}

	public boolean checkusername(String username) {
		boolean isexsist = true; // 声明用户名状态 初始值True 表示存在
		// 声明查询SQL语句
		String sql = "select count(*) from userinfo wherer username="
				+ username;

		try {
			// 创建查询Statement对象
			statement = connection.createStatement();
			reSet = statement.executeQuery(sql); // 执行查询方法 获取查询结果
			// 对查询结果进行判断
			reSet.next(); // 读取查询结果
			if (reSet.getInt(1) == 0) {
				isexsist = false; // false 表示用户名不存在 可以注册
				LogDog.logger.debug(username + "可以使用");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return isexsist;
	}

	public boolean login(User user) {
		boolean logonStatus = false; // 声明用户登录状态 初始值False 表示登录失败
		// 定义用户登录SQl语句
		String sql = "select count(*) from userinfo where username=? and userpass=?";

		try {
			// 从数据库连接创建PreparedStatement对象
			prStatement = connection.prepareStatement(sql);
			// 对产生的对象进行赋值
			prStatement.setString(1, user.getUsername()); // 用户名
			prStatement.setString(2, user.getUserpass()); // 密码
			// 查询SQL语句得到结果
			reSet = prStatement.executeQuery();
			reSet.next(); // 读取结果
			if (reSet.getInt(1) >= 0) {
				logonStatus = true;
				LogDog.logger.info(user.getUsername() + "--登录成功!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return logonStatus;
	}

	public User getuser(String username) {
		// 声明User 对象
		User user = new User();
		// 声明查询SQL语句
		String sql = "select * from userinfo where username='" + username + "'";
		// 创建查询对象
		try {
			statement = connection.createStatement();
			// 查询并返回结果
			reSet = statement.executeQuery(sql);
			if (reSet.next()) {
				user.setUid(reSet.getInt("uid"));
				user.setUsername(reSet.getString("username"));
				user.setUserpass(reSet.getString("userpass"));
				user.setNickname(reSet.getString("nickname"));
				user.setEmail(reSet.getString("email"));
				user.setTel(reSet.getString("tel"));
				user.setMobile(reSet.getString("mobile"));
				user.setqQ(reSet.getString("QQ"));
				user.setSex(reSet.getString("sex"));
				user.setBirthday(reSet.getString("birthday"));
				user.setAdress(reSet.getString("adress"));
				user.setRegistertime(reSet.getString("registerTime"));
				user.setSchool(reSet.getString("school"));
				// 创建星座对象
				AstroType as = new AstroType();
				as.setSid(reSet.getInt("sid"));
				as.setStype(reSet.getString("stype"));
				user.setAstro(as);
				// 创建血型对象
				BloodType bt = new BloodType();
				bt.setBid(reSet.getInt("bid"));
				bt.setBtype(reSet.getString("btype"));
				user.setBloodtype(bt);
				user.setDisc(reSet.getString("disc"));
				user.setStylewrite(reSet.getString("stylewrite"));
				// 所有数据赋值完毕
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user; // 返回User
	}

	public boolean modifyuser(User user) {
		/*
		 * 方法可能不安全 例如用户名为空
		 */
		// 声明是否修改成功变量
		boolean modifyStatus = false;
		// 声明修改用户资料SQL
		String sql = "update userinfo set nickname=?,email=?,tel=?,mobile=?,QQ=?,sex=?,birthday=?,"
				+ "adress=?,school=?,astro=?,bloodtype=?disc=?,stylewrite=?";
		// 产生PreparedStatement对象
		try {
			prStatement = connection.prepareStatement(sql);
			// 对对象进行赋值
			prStatement.setString(1, user.getNickname());
			prStatement.setString(2, user.getEmail());
			prStatement.setString(3, user.getTel());
			prStatement.setString(4, user.getMobile());
			prStatement.setString(5, user.getqQ());
			prStatement.setString(6, user.getSex());
			prStatement.setString(7, user.getBirthday());
			prStatement.setString(8, user.getAdress());
			prStatement.setString(9, user.getSchool());
			prStatement.setInt(10, user.getAstro().getSid());
			prStatement.setInt(11, user.getBloodtype().getBid());
			prStatement.setString(12, user.getDisc());
			prStatement.setString(13, user.getStylewrite());
			// 赋值完毕 执行修改操作
			int rows = prStatement.executeUpdate();
			if (rows > 0) {
				LogDog.logger.info(user.getUsername() + "--修改成功");
				modifyStatus = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return modifyStatus;
	}

	public boolean deleteuser(int userid) {
		// 定义删除状态
		boolean deleteStatus = false;
		// 定义删除用户SQL语句
		String sql = "delete from userinfo where userid=" + userid;
		try {
			statement = connection.createStatement();
			// 执行删除操作
			int rows = statement.executeUpdate(sql);
			if (rows > 0) {
				deleteStatus = true;
				LogDog.logger.info(userid + "--删除成功");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return deleteStatus;
	}

	public List<User> searchuser(String sql) {
		// 定义查询用户返回List
		List<User> list = new ArrayList<User>();
		// 获取对象查询
		try {
			statement = connection.createStatement();
			reSet = statement.executeQuery(sql);
			// 获取结果
			while (reSet.next()) {
				// 声明User 对象
				User user = new User();
				user.setUid(reSet.getInt("uid"));
				user.setUsername(reSet.getString("username"));
				user.setUserpass(reSet.getString("userpass"));
				user.setNickname(reSet.getString("nickname"));
				user.setEmail(reSet.getString("email"));
				user.setTel(reSet.getString("tel"));
				user.setMobile(reSet.getString("mobile"));
				user.setqQ(reSet.getString("QQ"));
				user.setSex(reSet.getString("sex"));
				user.setBirthday(reSet.getString("birthday"));
				user.setAdress(reSet.getString("adress"));
				user.setRegistertime(reSet.getString("registerTime"));
				user.setSchool(reSet.getString("school"));
				// 创建星座对象
				AstroType as = new AstroType();
				as.setSid(reSet.getInt("sid"));
				as.setStype(reSet.getString("stype"));
				user.setAstro(as);
				// 创建血型对象
				BloodType bt = new BloodType();
				bt.setBid(reSet.getInt("bid"));
				bt.setBtype(reSet.getString("btype"));
				user.setBloodtype(bt);
				user.setDisc(reSet.getString("disc"));
				user.setStylewrite(reSet.getString("stylewrite"));
				// 所有数据赋值完毕
				list.add(user); //添加到User 
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}

}
